clear all
set more off
set maxvar 10000


	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if baseline_sample==1
    gen number_wgted =1
    gen number_bydecade_bysex =1

/* # of survey respondents who report a particular father occupation,
   by decade and sex (weighted)  */
    preserve
        collapse (count) number_wgted [aw=wgt_sex_race], by(decade fatheroccej sex)
        order decade number_wgted fatheroccej sex 
        gsort decade sex -number_wgted

        bysort decade sex: gen tag = _n

        keep if inrange(tag,1,5)

        tempfile temp
        save `temp'
    restore

* # of survey respondents, by decade and sex (weighted) 
    collapse (count) number_bydecade_bysex [aw=wgt_sex_race], by(decade sex)
    order decade number_bydecade_bysex sex 

* Merge the two pieces of info together
    merge 1:m decade sex using `temp'
    drop _merge

    order decade fatheroccej sex number_wgted number_bydecade_bysex
    sort decade sex fatheroccej tag

* Father occupation labels (for table)
    gen fatheroccej_name = ""
    replace fatheroccej_name = "Public-school teachers" if fatheroccej==3
    replace fatheroccej_name = "Engineers" if fatheroccej==7
    replace fatheroccej_name = "Other professional and technical" if fatheroccej==17
    replace fatheroccej_name = "Businessman (self-employed)" if fatheroccej==21
    replace fatheroccej_name = "Businessman (not self-employed)" if fatheroccej==28
    replace fatheroccej_name = "Sales (inside)" if fatheroccej==35
    replace fatheroccej_name = "Craftsman (skilled)" if fatheroccej==48
    replace fatheroccej_name = "Craftsman (semi-skilled)" if fatheroccej==51
    replace fatheroccej_name = "Protective service officer" if fatheroccej==61
    replace fatheroccej_name = "Private household workers" if fatheroccej==65
    replace fatheroccej_name = "Other service workers" if fatheroccej==68
    replace fatheroccej_name = "Farm laborers" if fatheroccej==71
    replace fatheroccej_name = "Unskilled laborer (non-farm)" if fatheroccej==78    
    replace fatheroccej_name = "Farm operator" if fatheroccej==81
    tab fatheroccej_name, m

    gen share_m =.
    gen share_w =.
    
* Create table
    file open rb using "$Mydirectory2/appendix_c/rb_stata2tex_wgted.tex", write replace

    foreach d of numlist 1/7   {

        file write rb "\multirow{5}{*}{\normalsize{19`d'0s}}" _n

        forvalues x = 1/5 {

            levelsof fatheroccej_name if decade==19`d'0 & tag==`x' & sex==1, local(occlabelm) clean //Note: clean option removes compound double quotes
            levelsof fatheroccej_name if decade==19`d'0 & tag==`x' & sex==2, local(occlabelw) clean
            
        * Share male
            replace share_m = number_wgted/number_bydecade_bysex if decade==19`d'0 & tag==`x' & sex==1
            
            summ share_m if decade==19`d'0 & tag==`x' & sex==1
            local number_m = `r(mean)'
            di "`number_m'"
            local number_m2: display %-09.2fc `number_m'

        *Share female    
            replace share_w = number_wgted/number_bydecade_bysex if decade==19`d'0 & tag==`x' & sex==2
        
            summ share_w if decade==19`d'0 & tag==`x' & sex==2 
            local number_w = `r(mean)'
            di "`number_w'"
            local number_w2: display %-09.2fc `number_w'                            
            
            file write rb "& `x'. `occlabelm' & `number_m2' & `x'. `occlabelw' & `number_w2' \\" _n
        }

        file write rb "" _n
        
        if `d' <7  {
            file write rb "\addlinespace[3ex]" _n
            file write rb "\hline"  _n
            file write rb "\hline"  _n
            file write rb "\addlinespace[3ex]"  _n  
            file write rb "" _n
        } 
    }   
    
    file close rb